package com.achievement.service.impl;


import com.achievement.domain.dto.honor.ReviewSchemeDTO;
import com.achievement.domain.entity.ReviewSchemeEntity;
import com.achievement.domain.param.honor.ReviewSchemeParam;
import com.achievement.domain.vo.honor.ReviewSchemeVO;
import com.achievement.domain.vo.honor.ReviewStandardVO;
import com.achievement.mapper.ReviewSchemeMapper;
import com.achievement.mappings.ReviewSchemeMapping;
import com.achievement.mappings.ReviewStandardMapping;
import com.achievement.service.IReviewSchemeService;
import com.achievement.service.IReviewStandardService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReviewSchemeServiceImpl extends ServiceImpl<ReviewSchemeMapper, ReviewSchemeEntity> implements IReviewSchemeService {
    @Autowired
    private ReviewSchemeMapping mapping;
    @Autowired
    private ReviewStandardMapping standardMapping;
    @Autowired
    private IReviewStandardService reviewStandardService;
    /**
     * 获取成果评审方案
     * @param param
     * @return
     */
    @Override
    public List<ReviewSchemeVO> selectByPage(ReviewSchemeParam param) {
        LambdaQueryWrapper<ReviewSchemeEntity> queryWrapper = new LambdaQueryWrapper();
        //成果评审方案，businessType默认为honor
        param.setBusinessType("honor");
        List<ReviewSchemeVO> voList =  this.baseMapper.selectByPage(param);
        return voList;
    }
    @Override
    public List<ReviewSchemeEntity> getListByConditions(ReviewSchemeDTO patentDTO) {
        QueryWrapper<ReviewSchemeEntity> queryWrapper = new QueryWrapper();
        List<ReviewSchemeEntity> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public ReviewSchemeVO getInfoById(String id) {
        ReviewSchemeEntity entity = this.getById(id);
        ReviewSchemeVO reviewSchemeVO=mapping.entity2Vo(entity);
        List<ReviewStandardVO> reviewStandardVOs=reviewStandardService.getListByReviewSchemeId(id);
        reviewSchemeVO.setReviewStandardVOs(reviewStandardVOs);
        return reviewSchemeVO;
    }

    /**
     * 新增评审方案
     * @param dto
     * @return
     */
    @Override
    public boolean add(ReviewSchemeDTO dto) {
        //新增评审方案的时候，增加评审标准
        ReviewSchemeEntity entity = mapping.dto2Entity(dto);
        boolean insertResult=false;
        if(!dto.getReviewStandardDTOs().isEmpty()){
            //entity.setId(UUID.randomUUID().toString().replace("-",""));
            entity.setBusinessType("honor");
            if(this.save(entity)){
                boolean addStandard=reviewStandardService.bitchAdd(dto.getReviewStandardDTOs(),entity.getId());
                if(addStandard){
                    insertResult=true;
                }else{
                    //reviewStandardService.deleteReviewStandardByReviewSchemeId(entity.getId());
                    this.removeById(entity.getId());
                }
            }

        }
        return insertResult;
    }


    @Override
    public boolean update(ReviewSchemeDTO dto) {
        boolean updateResult=false;
        ReviewSchemeEntity entity = mapping.dto2Entity(dto);
            if(!dto.getReviewStandardDTOs().isEmpty()){
                if(reviewStandardService.deleteReviewStandardByReviewSchemeId(entity.getId())){
                    if(reviewStandardService.bitchAdd(dto.getReviewStandardDTOs(),entity.getId())){
                        updateResult=this.updateById(entity);//更新成功
                    }
                }
            }

        return updateResult;
    }

    @Override
    public boolean batchRemove(List<String> ids) {
        for (String id:ids
        ) {
            reviewStandardService.deleteReviewStandardByReviewSchemeId(id);
        }
        boolean returnResult=false;
        returnResult=this.removeByIds(ids);
        return returnResult;
    }
}
